package com.crm.controller.quotController;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.crm.dao.OpportunityDAO;
import com.crm.dao.QuotationDAO;
import com.crm.entity.Business;
import com.crm.entity.Opportunity;
import com.crm.entity.Quotation;
@Controller
@RequestMapping("/printquot")
public class PrintQuotController {
	protected final Log logger = LogFactory.getLog(getClass());
	@RequestMapping( "/show.do") 
	public ModelAndView show(HttpServletRequest req,HttpSession session, ModelAndView model) throws Exception{
		Business bus=(Business)session.getAttribute("business");
		if(bus==null)return new ModelAndView("/jsp/index.jsp");
		String tAndC=bus.getTandc();
		
			//OpportunityDAO oprD=new OpportunityDAO();
			
			List<Opportunity> opr=null;
			try{
				opr=(List<Opportunity>)OpportunityDAO.getSingleInstance().getEnqByStatusForPrint(bus.getBusinessId());
					logger.info("opportunity list is "+opr);
			}catch(NullPointerException e){
				e.printStackTrace();
			}

			  model = new ModelAndView("/jsp/PrintQuotation.jsp");
				model.addObject("opprtunity",opr);
				model.addObject("submenu", "quotation");
				model.addObject("menuName","quotation")
				.addObject("terms",tAndC);
				return model;
	
	}
	@RequestMapping( "/openQuotation.do") 
	public ModelAndView showAllQuot(HttpServletRequest request,HttpServletResponse response,HttpSession session, ModelAndView model) throws Exception{
		Business bus=(Business)session.getAttribute("business");
		if(bus==null)return new ModelAndView("/jsp/index.jsp");
		
		String refno=request.getParameter("refno");
		logger.info("the reference number is"+refno);
		
		//OpportunityDAO oprDAO=new OpportunityDAO();
		int rid=OpportunityDAO.getSingleInstance().getByRefNo(bus.getBusinessId(),refno.trim()).getId();
		
	 QuotationDAO quDAO=new QuotationDAO();
		List<Quotation> listquot= quDAO.getQuotByEnq(rid,bus.getBusinessId());
		model.addObject("listquot",listquot);
		model.setViewName("/jsp/PrintRes.jsp");
	return model;
	
	}
	
}
